const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const Course = sequelize.define('Course', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  title: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: '课程标题'
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '课程描述'
  },
  cover: {
    type: DataTypes.STRING(255),
    allowNull: true,
    comment: '课程封面'
  },
  teacher_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '教师ID'
  },
  category_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '分类ID'
  },
  price: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0,
    comment: '课程价格'
  },
  original_price: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0,
    comment: '原价'
  },
  duration: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '课程时长(分钟)'
  },
  lessons_count: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '课时数'
  },
  students_count: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '学生数'
  },
  views_count: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '观看次数'
  },
  likes_count: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '点赞数'
  },
  status: {
    type: DataTypes.ENUM('draft', 'published', 'archived'),
    defaultValue: 'draft',
    comment: '状态'
  },
  is_free: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
    comment: '是否免费'
  },
  is_recommend: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
    comment: '是否推荐'
  },
  is_hot: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
    comment: '是否热门'
  },
  tags: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: '标签'
  },
  requirements: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '学习要求'
  },
  objectives: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '学习目标'
  },
  published_at: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '发布时间'
  },
  created_at: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    comment: '更新时间'
  }
}, {
  tableName: 'courses',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      fields: ['teacher_id']
    },
    {
      fields: ['category_id']
    },
    {
      fields: ['status']
    },
    {
      fields: ['is_free']
    },
    {
      fields: ['is_recommend']
    },
    {
      fields: ['is_hot']
    }
  ]
});

// 关联关系在 models/index.js 中定义

module.exports = Course; 